package com.yanqu.road.dao.impl.activity.liondance;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.liondance.GameStatus;
import com.yanqu.road.entity.activity.liondance.Pile;
import com.yanqu.road.entity.activity.liondance.UserLionDanceData;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserLionDanceDataDaoImpl extends TempDataObjectDao<UserLionDanceData> {

    public static String getUserLionDanceDataSql = "select * from `t_u_activity_lion_dance_user_data` where `activityId`=? and `userId`=?";
    public UserLionDanceData getUserLionDanceData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserLionDanceDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }


    public static String updateUserLionDanceDataSql = "update `t_u_activity_lion_dance_user_data` set makeNum=?,process=?,combMap=?,cardMap=?,score=?,maxScore=?,maxRank=?,pileList=?,status=?" +
            ",totalUseNum=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserLionDanceData userLionDanceData) {
        userLionDanceData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userLionDanceData.getMakeNum()));
        para.put(i++, new DbParameter(Types.INTEGER, userLionDanceData.getProcess()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userLionDanceData.getCombMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(userLionDanceData.getCardMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userLionDanceData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, userLionDanceData.getMaxScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userLionDanceData.getMaxRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userLionDanceData.getPileList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userLionDanceData.getStatus())));
        para.put(i++, new DbParameter(Types.INTEGER, userLionDanceData.getTotalUseNum()));
        para.put(i++, new DbParameter(Types.INTEGER, userLionDanceData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userLionDanceData.getUserId()));
        if (getDb().executeNoneQuery(updateUserLionDanceDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserLionDanceDataSql = "insert into `t_u_activity_lion_dance_user_data`(activityId,userId,makeNum,process,combMap,cardMap,score,maxScore," +
            "maxRank,pileList,status,totalUseNum) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserLionDanceData userLionDanceData) {
        userLionDanceData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userLionDanceData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userLionDanceData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userLionDanceData.getMakeNum()));
        para.put(i++, new DbParameter(Types.INTEGER, userLionDanceData.getProcess()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userLionDanceData.getCombMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(userLionDanceData.getCardMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userLionDanceData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, userLionDanceData.getMaxScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userLionDanceData.getMaxRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userLionDanceData.getPileList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userLionDanceData.getStatus())));
        para.put(i++, new DbParameter(Types.INTEGER, userLionDanceData.getTotalUseNum()));
        if (getDb().executeNoneQuery(insertUserLionDanceDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserLionDanceData parseData(ResultSet rs) throws SQLException {
        UserLionDanceData data = new UserLionDanceData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setMakeNum(rs.getInt("makeNum"));
        data.setProcess(rs.getInt("process"));
        data.setCombMap(JSON.parseObject(rs.getString("combMap"), new TypeReference<Map<Integer, Integer>>() {}));
        Property property = PropertyHelper.parseStringToProperty(rs.getString("cardMap"));
        if(property != null) {
            data.setCardMap(property);
        }
        data.setScore(rs.getLong("score"));
        data.setMaxScore(rs.getLong("maxScore"));
        data.setMaxRank(rs.getInt("maxRank"));
        data.setPileList(JSON.parseObject(rs.getString("pileList"), new TypeReference<List<Pile>>() {}));
        data.setStatus(JSON.parseObject(rs.getString("status"), GameStatus.class));
        data.setTotalUseNum(rs.getInt("totalUseNum"));

        return data;
    }
}
